package com.hellpro.lolbox.moment.view;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hellpro.lolbox.R;
import com.hellpro.lolbox.base.BaseFragment;
import com.hellpro.lolbox.moment.bean.MomentContentBean;
import com.hellpro.lolbox.moment.model.MomentContentLvAdapter;
import com.hellpro.lolbox.moment.presenter.ContentPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 类描述:
 * 创建人:阿犇
 * 创建时间:2016/10/22
 * 备注:动态页面
 */
public class MomentContentFragment extends BaseFragment implements IMomentContentFragment {


    @BindView(R.id.moment_content_lv)
    PullToRefreshListView momentContentLv;
    private String url;
    private List<MomentContentBean.DataBean> data;
    private ContentPresenter presenter;
    private String url1;
    private String fv;
    private int page = 0;
    private MomentContentLvAdapter adapter;
    private List<MomentContentBean.DataBean> mList = new ArrayList<>();

    @Override
    public void initView() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_moment_content;
    }


    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 5) {
                MomentContentBean bean = (MomentContentBean) msg.obj;
                final List<MomentContentBean.DataBean> data = bean.getData();
//                presenter.setContentLvAdapter(data, flag);
                mList.addAll(data);
                adapter.setData(mList);

                momentContentLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        presenter.setContentLvListener(mList, parent, view, position);
                    }
                });
            }
        }
    };

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle bundle = getArguments();
        url = bundle.getString("url");
        String replace = url.replace("page=1", "");
        fv = replace.replace("fv", "page=");
        //处理数据
        presenter = new ContentPresenter(this);
        presenter.setLoadContentInfo(mHandler, 0);
        adapter = new MomentContentLvAdapter();
        momentContentLv.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        momentContentLv.setAdapter(adapter);
        momentContentLv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {

                page += 1;
                if (page <= 5) {
                    presenter.setLoadContentLastInfo(mHandler, 1);
                    momentContentLv.onRefreshComplete();
                } else {
                    Toast.makeText(getActivity(), "当前没有更多数据", Toast.LENGTH_SHORT).show();
                    momentContentLv.onRefreshComplete();
                }
            }
        });

    }

    @Override
    public PullToRefreshListView getMLv() {
        return momentContentLv;
    }

    @Override
    public String getMomentUrl() {

        return url;
    }

    @Override
    public String getMomentPageUrl() {
        return fv;
    }

    @Override
    public int getIntPage() {

        return page;
    }

    @Override
    public Context getMomentContext() {
        return getActivity();
    }

    @Override
    public List<MomentContentBean.DataBean> getContentData() {
        return data;
    }

}
